package io.kiki.sba.registry.common.model.slot.filter;


import com.google.common.collect.Sets;
import io.kiki.sba.registry.common.model.console.MultiSegmentSyncSwitch;
import io.kiki.sba.registry.common.model.constants.MultiValueConstants;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;


public class MultiSyncDataAcceptorManager implements Serializable {

    private static final long serialVersionUID = 1L;

    private Map<String, RemoteSyncDataAcceptorManager> dataCenterToRemoteSyncDataAcceptorManagerMap = new ConcurrentHashMap<>();

    public void updateFrom(Collection<MultiSegmentSyncSwitch> collection) {

        Map<String, RemoteSyncDataAcceptorManager> remoteManagers = new HashMap<>(collection.size());
        for (MultiSegmentSyncSwitch multiSegmentSyncSwitch : collection) {

            Set<SyncSlotAcceptor> set = Sets.newConcurrentHashSet();
            set.add(MultiValueConstants.DATUM_SYNCER_SOURCE_FILTER);
            set.add(new SyncPublisherGroupAcceptor(multiSegmentSyncSwitch.getSynPublisherGroups()));
            set.add(new SyncSlotDataInfoIdAcceptor(multiSegmentSyncSwitch.getSyncDataInfoIds(), multiSegmentSyncSwitch.getIgnoreDataInfoIds()));
            RemoteSyncDataAcceptorManager remoteSyncDataAcceptorManager = new RemoteSyncDataAcceptorManager(set);
            remoteManagers.put(multiSegmentSyncSwitch.getRemoteDataCenter(), remoteSyncDataAcceptorManager);
        }

        synchronized (this) {
            this.dataCenterToRemoteSyncDataAcceptorManagerMap = remoteManagers;
        }
    }

    public synchronized SyncSlotAcceptorManager getSyncSlotAcceptorManager(String dataCenter) {
        return dataCenterToRemoteSyncDataAcceptorManagerMap.get(dataCenter);
    }

    public final class RemoteSyncDataAcceptorManager extends BaseSyncSlotAcceptorManager {

        public RemoteSyncDataAcceptorManager(Set<SyncSlotAcceptor> set) {
            super(set);
        }
    }
}
